<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/19 0019
 * Time: 下午 1:12
 */

namespace app\index\controller;

use app\admin\model\Product;
use app\admin\model\Indent;
use app\admin\model\Specification;
use app\admin\model\User;
use app\index\model\IndentIntegral;
use app\index\model\IntegralUsed;
use think\Db;
use think\Session;

class Extension extends Common
{
    /**
     * @title   推广首页
     */
    public function index()
    {
        //获取用户的类型
        $u_type = (new User())->where('id',Session::get('userid'))->value('u_type');
        $uid = Session::get('userid');
        if($u_type==1){
            $integral = integral($uid,2);
            $this->assign('tz_integral',$integral);
        }else if($u_type==2){
            $integral = integral($uid,2);
            $this->assign('tz_integral',$integral);

            $integral = integral($uid,1);
            $this->assign('mf_integral',$integral);
        }
        return $this->fetch('index/reward');
    }

    /**
     * @title   推广列表
     * @return mixed
     */
    public function extenList($status=null,$type=null)
    {
        //获取用户类型
        $user = new User();
        $u_type = $user->where('id',Session::get('userid'))->value('u_type');
        $ii = new IndentIntegral();
        $product = new Product();
        $spec = new Specification();
        $list = '';
        $list['u_type'] = $u_type;
        switch ($status){
            case '1':
                $where['i_type'] = 2;//套装
                break;
            case '2':
                $where['i_type'] = 1;//免费领取
                break;
        }
        switch ($type){
            case '0':
                $where['i_status'] = 0;
                $where1['i_status'] = 0;
                break;
            case '1':
                $where['i_status'] = 1;
                $where1['i_status'] = 1;
                break;
            case '2':
                $where['i_confirm'] = 2;
                $where1['i_confirm'] = 2;
                break;
        }
        $where['complete'] = 1;
        $where['user_id'] = Session::get('userid');
        $list['data'] = $ii->alias('ii')->where($where)->join('__INDENT__ i','i.id=ii.indent_id')->order('create_time desc')->select();
        foreach ($list['data'] as $key => $value){
            $list['data'][$key]['product'] = $product->field('id,p_name,p_photo')->where('id',$value['i_pid'])->find()->toArray();
            $list['data'][$key]['spec'] = $spec->field('s_name,s_price')->where('id',$value['i_spec'])->find();
            $list['data'][$key]['exten'] = (new User())->where('id',$value['i_uid'])->value('u_nickname');
        }
//        dump($list);die;
        $this->assign('list',$list);
        return $this->fetch('index/extension');
    }

    public function introduce()
    {
        return $this->fetch('index/extension_rules');
    }
}